#include "widget.h"
#include "ui_widget.h"
#include <QDateTime>
#include <QDebug>
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    //设置旋钮属性及初始值
    ui->changeOpacityDial->setNotchesVisible(true);
    ui->changeOpacityDial->setWrapping(true);
    ui->changeOpacityDial->setMaximum(100);
    ui->changeOpacityDial->setMinimum(0);
    ui->changeOpacityDial->setValue(100);

    //设置滑动条属性及初始值
    //ui->horizontalSlider->setTickPosition(QSlider::TicksBothSides);
    ui->horizontalSlider->setMinimum(this->width());
    ui->horizontalSlider->setMaximum(1500);
    ui->horizontalSlider->setValue(this->width());

    ui->verticalSlider->invertedAppearance();
   //ui->verticalSlider->setTickPosition(QSlider::TicksBothSides);
    ui->verticalSlider->setMinimum(this->height());
    ui->verticalSlider->setMaximum(1200);
    ui->verticalSlider->setValue(this->height());

}

Widget::~Widget()
{
    delete ui;
}


//注意调用daysTo的应该始终是开始时间
void Widget::on_startDateTimeEdit_dateTimeChanged(const QDateTime &dateTime)
{
    QDateTime endDateTime = ui->endDateTimeEdit->dateTime();
    qint64 days = dateTime.daysTo(endDateTime);
    qint64 secs = dateTime.secsTo(endDateTime);    //返回值相当于将天数转换为了秒数
    qint64 hours = secs / 3600 % 24;
    QString res = "相差了" + QString::fromStdString(std::to_string(days)) + "天" + QString::fromStdString(std::to_string(hours)) + "时";
    ui->resultLabel->setText(res);
}

void Widget::on_endDateTimeEdit_dateTimeChanged(const QDateTime &dateTime)
{
    QDateTime startDateTime = ui->startDateTimeEdit->dateTime();
    qint64 days = startDateTime.daysTo(dateTime);
    qint64 secs = startDateTime.secsTo(dateTime);
    qint64 hours = secs / 3600 % 24;
    QString res = "相差了" + QString::fromStdString(std::to_string(days)) + "天" + QString::fromStdString(std::to_string(hours)) + "时";
    ui->resultLabel->setText(res);
}

//改变窗口的不透明度
void Widget::on_changeOpacityDial_valueChanged(int value)
{
    double opacity = (double)value / 100;
    this->setWindowOpacity(opacity);
}

void Widget::on_horizontalSlider_valueChanged(int value)
{
    qDebug() << this->x() << " " << this->y() << " " << value << " "<< this->height();
    //this->setGeometry(this->x(),this->y(), value, this->height());    这样会有bug，why?
    QRect rect = this->geometry();
    this->setGeometry(rect.x(), rect.y(), value, rect.height());
}


void Widget::on_verticalSlider_valueChanged(int value)
{
    qDebug() << this->x() << " " << this->y() << " " << value << " "<< this->width();
    //this->setGeometry(this->x(),this->y(), this->width(), value);     这样会有bug，why?
    QRect rect = this->geometry();
    this->setGeometry(rect.x(),rect.y(),rect.width(),value);
}
